package com.d404.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import im.beans.Message;
import im.notify.server.Notify;
import manager.MessageManager;
import manager.PersistentManager;

/**
 * Servlet implementation class SendPlainMessage
 */
//@WebServlet("/SendMessage")
public class SendMessage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SendMessage() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		
		Download download = new Download();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
		Session session = sf.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try{
			Message msg = getMessage(upload);
			session.save(msg);
			session.getTransaction().commit();
			// 发送简单消息
			MessageManager.sendSimpleMessage(upload.getToName(), Notify.PLAIN_MESSAGE);
			download.setStatus(Download.SUCCESS);
		}catch(Exception e)
		{
			tx.rollback();
			e.printStackTrace();
			download.setStatus(Download.FAILD);
		}finally {
		}
		System.out.println(JSON.toJSONString(download));
		response.setCharacterEncoding("UTF-8");
		response.getWriter().append(JSON.toJSONString(download));
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	// 获取存储消息
	Message getMessage(Upload upload)
	{
		Message msg = new Message();
		msg.setFileName(upload.getFileName());
		msg.setFileURI(upload.getFileURI());
		msg.setFrom_id(upload.getFrom_id());
		msg.setFromName(upload.getFromName());
		msg.setIsRead(false);
		msg.setText(upload.getText());
		msg.setTimestamp(upload.getTimestamp());
		msg.setTo_id(upload.getTo_id());
		msg.setToName(upload.getToName());
		msg.setType(upload.getType());
		msg.setTimestamp(System.currentTimeMillis()+"");
		return msg;
	}
	
	
	// 上传对象
    public static class Upload
    {
        // fromME
        public static final int FROMME = 1;
        public static final int FROMREMOTE = 2;
        // type
        public static final int TYPE_PLANIMESSAGE = 1;
        public static final int TYPE_FILE = 2;
        // local
        public static final int LOCAL = 1;
        public static final int REMOTE = 2;

        long _id;
        // 发送消息者id
        long from_id;
        // 发送消息者
        String fromName;
        // 接受消息者id
        long to_id;
        // 接受消息者名
        String toName;
        // 是否是自己发的
        int fromMe;
        // 消息类型
        int type;
        // 消息是文件，地址
        String fileURI;
        // 文件名
        String fileName;
        // 是本机文件？
        int local;
        // 消息文本
        String text;
        // 时间戳
        String timestamp;

        public long get_id() {
            return _id;
        }

        public void set_id(long _id) {
            this._id = _id;
        }

        public long getFrom_id() {
            return from_id;
        }

        public void setFrom_id(long from_id) {
            this.from_id = from_id;
        }

        public String getFromName() {
            return fromName;
        }

        public void setFromName(String fromName) {
            this.fromName = fromName;
        }

        public long getTo_id() {
            return to_id;
        }

        public void setTo_id(long to_id) {
            this.to_id = to_id;
        }

        public String getToName() {
            return toName;
        }

        public void setToName(String toName) {
            this.toName = toName;
        }

        public int getFromMe() {
            return fromMe;
        }

        public void setFromMe(int fromMe) {
            this.fromMe = fromMe;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getFileURI() {
            return fileURI;
        }

        public void setFileURI(String fileURI) {
            this.fileURI = fileURI;
        }

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public int getLocal() {
            return local;
        }

        public void setLocal(int local) {
            this.local = local;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }
    }
    
    public static class Download
    {
    	public static final String SUCCESS = "1";
    	public static final String FAILD = "2";
    	
    	String status;

		public String getStatus() {
			return status;
		}

		public void setStatus(String status) {
			this.status = status;
		}
    	
    }

}
